home *** CD-ROM | disk | FTP | other *** search
- /* SerialCommDevice.h
-
- This class defines an easy to use, robust, fault-tollerant, full-duplex serial
- communications protocol. The implementation uses a packet-based protocol that
- transmits only printable ascii characters.
-
- Automatic retries insure that this protocol doesn't fall flat on it's
- face whenever a packet fails to be successfully received. There is both
- a recieve timeout, and a transmit timeout. The transmit timeout limits
- the length of time that the sender will wait for an ack to a data packet
- transfer, while the recieve timeout limits the amount of time that a
- read waits for a valid data packet to be received. The default setting
- for recvTimeoutMsecs is 0 (not timeout) and xmitAckTimeoutMsecs is 250
- (1/4 sec).
-
- If a xmit timeout occures the data packet is re-sent to the remote
- interface. If a received packet has a bad checksum, then a NACK (negative
- acknowledge) packet is immediatly transmitted. Failing xmit transfers
- (either due to timeout or receipt of NACK packet, will be automatically
- retried until the maxXmitRetries have been attempted. The default setting
- for maxXmitRetries is 60, making 15 seconds the longest amount of time a
- transfer will be attempted for a broken network. Both timeouts and retries
- can be easily modified with the provided methods.
- */
-
- #import <objc/Object.h>
- #import <sys/time.h>
-
- typedef enum {
- noPort,
- sccPortA,
- sccPortB
- } sccPortType;
- extern char *portString(sccPortType thePort); /* returns name of the specified port. */
-
- typedef enum {
- brNone=0,
- br50=1,
- br75=2,
- br110=3,
- br134=4,
- br150=5,
- br200=6,
- br300=7,
- br600=8,
- br1200=9,
- br1800=10,
- br2400=11,
- br4800=12,
- br9600=13,
- br19200=14,
- br38400=15,
- } baudRateType;
-
- extern int microTimeStampA(struct timeval * oldTime);
-
- #define BR_MIN br50
- #define BR_MIN_AUTO_CONF br1200
- #define BR_MAX br38400
- #define SCD_MAX_PACKETSIZE (1024*8) /* maximum packet size - some characters reserved to define pkt. */
- #define SCD_RECV_BUFFERS 2
-
- /* error codes: */
- #define SCD_TIMEOUT_ERR (-1) /* receive timeout expired. */
- #define SCD_IO_ERR (-2) /* read/write/select failure. */
- #define SCD_XFER_ERR (-3) /* not able to transfer packet. */
- #define SCD_LENGTH_ERR (-4) /* Data too large for buffer. */
- #define SCD_OPEN_ERR (-5) /* open/ioctl failed on tty port. */
-
- @interface SerialCommDevice : Object
- {
- int fd;
- int xmitPktId;
- int dataPktLen;
- int dataPktIndex;
- int nackDPktFlag;
- int asciiRcvDPktID;
- int asciiAckDPktID;
- int maxXmitRetries;
- int recvTimeoutMsecs;
- int xmitAckTimeoutMsecs;
- sccPortType port;
- baudRateType baudRate;
- char *writeLock;
- char *dataPktLock;
- id delegate;
- char recvBuf[SCD_RECV_BUFFERS][SCD_MAX_PACKETSIZE];
- }
-
- - setRecvTimeoutMsecs:(int)msecs;
- - (int)recvTimeoutMsecs;
- - setXmitTimeoutMsecs:(int)msecs;
- - (int)xmitTimeoutMsecs;
- - setmaxXmitRetries:(int)retries;
- - (int)maxXmitRetries;
-
- /* xmit:,...
- *
- * Send string to remote and wait for ack. Returns negative error code or
- * positive number of microseconds required for transfer.
- */
- - (int)xmit:(char *)fmt, ...;
-
-
-
- /* xmit:len:
- *
- * Send fixed-length binary character array to remote and wait for ack. Returns
- * negative error code if failure, otherwise returns positive number of milliseconds
- * required for transfer.
- */
- - (int)xmit:(char *)buf len:(int)len;
-
-
-
- /* xmitOOL:,...
- *
- * Send string to remote and wait for ack. Returns negative error code or
- * positive number of microseconds required for transfer. If remote has
- * a delegate, the delegate is sent the data via the -oolMessage:len: msg.
- *
- */
- - (int)xmitOOL:(char *)fmt, ...;
-
-
-
- /* xmitOOL:len:
- *
- * Send fixed-length binary character array to remote and wait for ack. Returns
- * negative error code if failure, otherwise returns positive number of milliseconds
- * required for transfer. If remote has a delegate, the delegate is sent the data
- * via the -oolMessage:len: msg.
- */
- - (int)xmitOOL:(char *)buf len:(int)len;
-
-
- /* test:,...
- *
- * Like xmit:,..., but sends a test packet instead of a data packet. Test
- * packets are immediatly acknowledged by the remote's receiving thread and
- * are not forwarded to the receiving application.
- * Returns negative error code or positive number of microseconds
- * required for transfer.
- */
- - (int)test:(char *)fmt, ...;
-
-
-
- /* test:len:
- *
- * Like xmit:len:, but sends a test packet instead of a data packet. Test
- * packets are immediatly acknowledged by the remote's receiving thread (if
- * it is up and running), and the packet is not forwarded to the receiving
- * application. Returns negative error code or positive number of
- * microseconds required for transfer.
- */
- - (int)test:(char *)buf len:(int)len;
-
-
- /* -initPort:
- * Open specified port and walk thru baud rates until one is
- * found where remote acknoledges xfer (via -test:). If no
- * acks are ever generated, then a default(38.4Kbaud) is selected.
- */
- - initPort:(sccPortType)thePort ;
-
-
- /* pollRecvBufferReady
- *
- * Returns 1 if recv Buffer contains data and is ready to be read, otherwise returns 0.
- */
- - (int)pollRecvBufferReady;
-
- /* recv:maxLen:
- *
- * Returns negative error code if failure, otherwise returns positive
- * number of characters received.
- */
- - (int)recv:(char *)str maxLen:(int)len;
-
-
-
- /* - setPort:atRate:
- *
- * Change port and/or transmission parameters.
- * Return 0 on success and non-zero if fail.
- */
- - (int)setPort:(sccPortType)thePort atRate:(baudRateType)theRate;
-
-
-
-
- /* - initPort:atRate:
- *
- * open specified port at specified rate. Returns nil on failure.
- */
- - initPort:(sccPortType)thePort atRate:(baudRateType)theRate;
-
-
- /* - initPort:
- *
- * open specified port and search for match baud rate. Returns nil on failure.
- */
- - initPort:(sccPortType)thePort;
-
-
- /* - init
- *
- * open default port (A) and search for match baud rate. Returns nil on failure.
- */
- - init;
-
- -setDelegate:myDelegate;
- -delegate;
- @end
-
- @interface SCDDelegate : Object
- {}
- /* - oolMessage:len:
- *
- * An asyncronous message that can be generated by this object at any time. Comes in the
- * context of the background thread that is normally used to read data off the serial port
- * (therefore can not be used to draw or to call any of this object's -xmit or -recv
- * methods). Note that the delegate's method should return reasonably quickly in order
- * for the thread to continue to receive new data. WARNING: The contents of bufr can
- * change as soon as this method returns.
- */
- -oolMessage:(char *)bufr len:(int)l;
- @end
-